package com.soft.bams.server;

import com.soft.bams.service.Bank;
import com.soft.bams.vo.RankVO;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class RankPanel extends JPanel {
    private ATMServer atmServer;
    private Bank bank = Bank.getInstance();

    private JTable dataTable;
    private DefaultTableModel tableModel;

    private JButton totalLoanBtn = new JButton("贷款总额");
    private JButton totalBalanceBtn = new JButton("余额总数");
    private JButton totalCeilingBtn = new JButton("透支总额");
    private JButton userRankBtn = new JButton("用户排名");
    private JButton accountsIpBtn = new JButton("IP信息");
    private JButton backBtn = new JButton("返回");

    public RankPanel(ATMServer atmServer) {
        this.atmServer = atmServer;
        setLayout(null);
        initComponents();
    }

    public void initTableModel(String[] columnNames){
        tableModel = new DefaultTableModel(columnNames, 0);
        dataTable = new JTable(tableModel);


        JScrollPane scrollPane = new JScrollPane(dataTable);
        scrollPane.setBounds(50, 100, 700, 350);
        add(scrollPane);
    }

    private void initComponents() {
        initTableModel(new String[]{"统计的数据","total"});

        // 设置按钮
        totalLoanBtn.setBounds(50, 30, 150, 40);
        totalLoanBtn.addActionListener(e -> showTotalLoan());
        add(totalLoanBtn);

        totalBalanceBtn.setBounds(220, 30, 150, 40);
        totalBalanceBtn.addActionListener(e -> showTotalBalance());
        add(totalBalanceBtn);

        totalCeilingBtn.setBounds(390, 30, 150, 40);
        totalCeilingBtn.addActionListener(e -> showTotalCeiling());
        add(totalCeilingBtn);

        userRankBtn.setBounds(560, 30, 150, 40);
        userRankBtn.addActionListener(e -> showUserRank());
        add(userRankBtn);

        accountsIpBtn.setBounds(450,480,100,40);
        accountsIpBtn.addActionListener(e -> showAccountsIpInfo());
        add(accountsIpBtn);

        backBtn.setBounds(300, 480, 100, 40);
        backBtn.addActionListener(e -> atmServer.showPanel("Server"));
        add(backBtn);

        // 添加标题
        JLabel titleLabel = new JLabel("数据统计 - 上帝视角", JLabel.CENTER);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
        titleLabel.setBounds(0, 0, 800, 30);
        add(titleLabel);
    }

    private void showTotalLoan() {
        initTableModel(new String[]{"统计的数据","total"});

        long total = bank.getTotalLoanAmount();
        tableModel.addRow(new Object[]{
                "所有账户贷款总额",
                total
        });
    }

    private void showTotalBalance() {
        initTableModel(new String[]{"统计的数据","total"});

        double total = bank.getTotalBalance();
        tableModel.addRow(new Object[]{
                "所有账户余额总数",
                total
        });
    }

    private void showTotalCeiling() {
        initTableModel(new String[]{"统计的数据","total"});

        double total = bank.getTotalCeiling();
        tableModel.addRow(new Object[]{
                "所有信用账户透支总额",
                total
        });
    }

    private void showUserRank() {
        initTableModel(new String[]{"序号","personId", "total"});

        TreeSet<RankVO> userRank = bank.userAssetRank();

        int rank = 1;
        for (RankVO rankVO : userRank) {
            tableModel.addRow(new Object[]{
                    rank++,
                    rankVO.getPersonId(),
                    rankVO.getTotal()
            });
        }
    }

    private void showAccountsIpInfo(){
        initTableModel(new String[]{"序号","账户ID","IP地址"});
        Set<Map.Entry<Long, String>> accountsIpInfo = bank.getAccountsIpInfo();
        int rank = 1;
        for (Map.Entry<Long, String> accountsIpInfoEntry : accountsIpInfo) {
            tableModel.addRow(new Object[]{
                    rank++,
                    accountsIpInfoEntry.getKey(),
                    accountsIpInfoEntry.getValue()
            });
        }
    }
}
